<template><div><h2 id="资源" tabindex="-1"><a class="header-anchor" href="#资源"><span>资源</span></a></h2>
<p>RESTful API 都是关于访问和操作 <em>资源（resources）</em> 的。您可以将资源当作 MVC 范例中的模型（models）来看。</p>
<p>While there is no restriction in how to represent a resource, in Yii you usually would represent resources in terms of objects of [[yii\base\Model]] or its child classes (e.g. <code v-pre>yii\db\ActiveRecord</code>), for the following reasons:</p>
<ul>
<li><code v-pre>yii\base\Model</code> implements the [[yii\base\Arrayable]] interface, which allows you to customize how you want to expose resource data through RESTful APIs.</li>
<li><code v-pre>yii\base\Model</code> supports <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/input-validation.md" target="_blank" rel="noopener noreferrer">input validation</a>, which is useful if your RESTful APIs need to support data input.</li>
<li><code v-pre>yii\db\ActiveRecord</code> provides powerful DB data access and manipulation support, which makes it a perfect fit if your resource data is stored in databases.</li>
</ul>
<p>In this section, we will mainly describe how a resource class extending from <code v-pre>yii\base\Model</code> (or its child classes) can specify what data may be returned via RESTful APIs. If the resource class does not extend from <code v-pre>yii\base\Model</code>, then all its public member variables will be returned.</p>
<h2 id="字段" tabindex="-1"><a class="header-anchor" href="#字段"><span>字段</span></a></h2>
<p>When including a resource in a RESTful API response, the resource needs to be serialized into a string. Yii breaks this process into two steps. First, the resource is converted into an array by <code v-pre>yii\rest\Serializer</code>. Second, the array is serialized into a string in a requested format (e.g. JSON, XML) by [[yii\web\ResponseFormatterInterface|response formatters]]. The first step is what you should mainly focus when developing a resource class.</p>
<p>By overriding [[yii\base\Model::fields()|fields()]] and/or [[yii\base\Model::extraFields()|extraFields()]], you may specify what data, called <em>fields</em>, in the resource can be put into its array representation. The difference between these two methods is that the former specifies the default set of fields which should be included in the array representation, while the latter specifies additional fields which may be included in the array if an end user requests for them via the <code v-pre>expand</code> query parameter. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// returns all fields as declared in fields()</span></span>
<span class="line">http<span class="token punctuation">:</span><span class="token comment">//localhost/users</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// only returns "id" and "email" fields, provided they are declared in fields()</span></span>
<span class="line">http<span class="token punctuation">:</span><span class="token comment">//localhost/users?fields=id,email</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// returns all fields in fields() and field "profile" if it is in extraFields()</span></span>
<span class="line">http<span class="token punctuation">:</span><span class="token comment">//localhost/users?expand=profile</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// returns all fields in fields() and "author" from post if</span></span>
<span class="line"><span class="token comment">// it is in extraFields() of post model</span></span>
<span class="line">http<span class="token punctuation">:</span><span class="token comment">//localhost/comments?expand=post.author</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// only returns "id" and "email" provided they are in fields() and "profile" if it is in extraFields()</span></span>
<span class="line">http<span class="token punctuation">:</span><span class="token comment">//localhost/users?fields=id,email&amp;expand=profile</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="覆盖-fields-方法" tabindex="-1"><a class="header-anchor" href="#覆盖-fields-方法"><span>覆盖 fields() 方法</span></a></h3>
<p>By default, [[yii\base\Model::fields()]] returns all model attributes as fields, while [[yii\db\ActiveRecord::fields()]] only returns the attributes which have been populated from DB.</p>
<p>You can override <code v-pre>fields()</code> to add, remove, rename or redefine fields. The return value of <code v-pre>fields()</code> should be an array. The array keys are the field names, and the array values are the corresponding field definitions which can be either property/attribute names or anonymous functions returning the corresponding field values. In the special case when a field name is the same as its defining attribute name, you can omit the array key. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// explicitly list every field, best used when you want to make sure the changes</span></span>
<span class="line"><span class="token comment">// in your DB table or model attributes do not cause your field changes (to keep API backward compatibility).</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">fields</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// field name is the same as the attribute name</span></span>
<span class="line">        <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// field name is "email", the corresponding attribute name is "email_address"</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email_address'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// field name is "name", its value is defined by a PHP callback</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$model</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$model</span><span class="token operator">-></span><span class="token property">first_name</span> <span class="token operator">.</span> <span class="token string single-quoted-string">' '</span> <span class="token operator">.</span> <span class="token variable">$model</span><span class="token operator">-></span><span class="token property">last_name</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// filter out some fields, best used when you want to inherit the parent implementation</span></span>
<span class="line"><span class="token comment">// and exclude some sensitive fields.</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">fields</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$fields</span> <span class="token operator">=</span> <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">fields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// remove fields that contain sensitive information</span></span>
<span class="line">    <span class="token keyword">unset</span><span class="token punctuation">(</span><span class="token variable">$fields</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'auth_key'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$fields</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'password_hash'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$fields</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'password_reset_token'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$fields</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Warning: Because by default all attributes of a model will be included in the API result, you should examine your data to make sure they do not contain sensitive information. If there is such information, you should override <code v-pre>fields()</code> to filter them out. In the above example, we choose to filter out <code v-pre>auth_key</code>, <code v-pre>password_hash</code> and <code v-pre>password_reset_token</code>.</p>
</blockquote>
<h3 id="覆盖-extrafields-方法" tabindex="-1"><a class="header-anchor" href="#覆盖-extrafields-方法"><span>覆盖 extraFields() 方法</span></a></h3>
<p>By default, [[yii\base\Model::extraFields()]] returns an empty array, while [[yii\db\ActiveRecord::extraFields()]] returns the names of the relations that have been populated from DB.</p>
<p>The return data format of <code v-pre>extraFields()</code> is the same as that of <code v-pre>fields()</code>. Usually, <code v-pre>extraFields()</code> is mainly used to specify fields whose values are objects. For example, given the following field declaration,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">fields</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">extraFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'profile'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>the request with <code v-pre>http://localhost/users?fields=id,email&amp;expand=profile</code> may return the following JSON data:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"id"</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"email"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"100@example.com"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"profile"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token string double-quoted-string">"id"</span><span class="token punctuation">:</span> <span class="token number">100</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string double-quoted-string">"age"</span><span class="token punctuation">:</span> <span class="token number">30</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="链接" tabindex="-1"><a class="header-anchor" href="#链接"><span>链接</span></a></h2>
<p><a href="https://en.wikipedia.org/wiki/HATEOAS" target="_blank" rel="noopener noreferrer">HATEOAS</a>, an abbreviation for Hypermedia as the Engine of Application State, promotes that RESTful APIs should return information that allows clients to discover actions supported for the returned resources. The key of HATEOAS is to return a set of hyperlinks with relation information when resource data are served by the APIs.</p>
<p>Your resource classes may support HATEOAS by implementing the [[yii\web\Linkable]] interface. The interface contains a single method [[yii\web\Linkable::getLinks()|getLinks()]] which should return a list of [[yii\web\Link|links]]. Typically, you should return at least the <code v-pre>self</code> link representing the URL to the resource object itself. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>base<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Link</span><span class="token punctuation">;</span> <span class="token comment">// represents a link object as defined in JSON Hypermedia API Language.</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Linkable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>Url</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserResource</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span> <span class="token keyword">implements</span> <span class="token class-name">Linkable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$id</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$email</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">//...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">fields</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">extraFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'profile'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getLinks</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token class-name static-context">Link</span><span class="token operator">::</span><span class="token constant">REL_SELF</span> <span class="token operator">=></span> <span class="token class-name static-context">Url</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'user/view'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'edit'</span> <span class="token operator">=></span> <span class="token class-name static-context">Url</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'user/view'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'profile'</span> <span class="token operator">=></span> <span class="token class-name static-context">Url</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'user/profile/view'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'index'</span> <span class="token operator">=></span> <span class="token class-name static-context">Url</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>When a <code v-pre>UserResource</code> object is returned in a response, it will contain a <code v-pre>_links</code> element representing the links related to the user, for example,</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"id"</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"email"</span><span class="token operator">:</span> <span class="token string">"user@example.com"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string">"_links"</span> => <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"self"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"href"</span><span class="token operator">:</span> <span class="token string">"https://example.com/users/100"</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"edit"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"href"</span><span class="token operator">:</span> <span class="token string">"https://example.com/users/100"</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"profile"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"href"</span><span class="token operator">:</span> <span class="token string">"https://example.com/users/profile/100"</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"index"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"href"</span><span class="token operator">:</span> <span class="token string">"https://example.com/users"</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数据集合" tabindex="-1"><a class="header-anchor" href="#数据集合"><span>数据集合</span></a></h2>
<p>Resource objects can be grouped into <em>collections</em>. Each collection contains a list of resource objects of the same type.</p>
<p>While collections can be represented as arrays, it is usually more desirable to represent them as <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/output-data-providers.md" target="_blank" rel="noopener noreferrer">data providers</a>. This is because data providers support sorting and pagination of resources, which is a commonly needed feature for RESTful APIs returning collections. For example, the following action returns a data provider about the post resources:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>rest<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>data<span class="token punctuation">\</span>ActiveDataProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">app<span class="token punctuation">\</span>models<span class="token punctuation">\</span>Post</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">actionIndex</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ActiveDataProvider</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'query'</span> <span class="token operator">=></span> <span class="token class-name static-context">Post</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>When a data provider is being sent in a RESTful API response, [[yii\rest\Serializer]] will take out the current page of resources and serialize them as an array of resource objects. Additionally, [[yii\rest\Serializer]] will also include the pagination information by the following HTTP headers:</p>
<ul>
<li><code v-pre>X-Pagination-Total-Count</code>: The total number of resources;</li>
<li><code v-pre>X-Pagination-Page-Count</code>: The number of pages;</li>
<li><code v-pre>X-Pagination-Current-Page</code>: The current page (1-based);</li>
<li><code v-pre>X-Pagination-Per-Page</code>: The number of resources in each page;</li>
<li><code v-pre>Link</code>: A set of navigational links allowing client to traverse the resources page by page.</li>
</ul>
<p>Since collection in REST APIs is a data provider, it shares all data provider features i.e. pagination and sorting.</p>
<p>An example may be found in the <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/rest-quick-start.md#trying-it-out" target="_blank" rel="noopener noreferrer">Quick Start</a> section.</p>
<h3 id="过滤集合" tabindex="-1"><a class="header-anchor" href="#过滤集合"><span>过滤集合</span></a></h3>
<p>Since version 2.0.13 Yii provides a facility to filter collections. An example can be found in the <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/rest-quick-start.md#trying-it-out" target="_blank" rel="noopener noreferrer">Quick Start</a> guide. In case you’re implementing an endpoint yourself, filtering could be done as described in <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/output-data-providers.md#filtering-data-providers-using-data-filters" target="_blank" rel="noopener noreferrer">Filtering Data Providers using Data Filters</a> section of Data Providers guide.</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


